/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

include( "shared.lua" )
include( "PP_shared.lua" )

require( "datastream" )


//Possibly add a way to grab the current limits either here or in the shared. PROBABLY THE SHARED

local PANEL = {}
local OldSelected = ""
local LimTable = LTable
local OldSentBool = false
local Saving, DoClose = false

function PANEL:Init()
	//Create controls here
	
	self:SetTitle( "Player Limits" )	//Add a close button that does not save OR try action signals again.
	self:ShowCloseButton( false )
	RunConsoleCommand( "SendPlayerLimits" )
	
	local RanksBox = vgui.Create( "DComboBox", self )
		RanksBox:SetMultiple( false )
		
	self.RanksBox = RanksBox
	
	self.Lims = {}	//Make the table for the text entries
	
	for k,v in pairs( RankTable ) do
		self:AddRanks( v )
	end
	
	local LimitsList = vgui.Create( "DPanelList", self )
		LimitsList:EnableHorizontal( false )
		LimitsList:EnableVerticalScrollbar( true )
		LimitsList:SetPadding( 5 )
		LimitsList:SetSpacing( 5 )
		
	self.LimitsList = LimitsList
	
	local SaveButton = vgui.Create( "DButton", self )
	local SPanel = self
		SaveButton:SetText( "Save and Exit" )
		function SaveButton:DoClick()
			datastream.StreamToServer( "SaveLimitsStream", { LimTable } )
			PrintTable( LimTable )
			Saving = true
		end
		
	self.SaveButton = SaveButton
	
	//Work on this later to make the default values for these limit controls the same as the rank you are changing.
	for k,v in pairs( LimitsTable ) do
		self:AddLimitControls( v )
	end

	self:MakePopup()
	self:Center()	//Make sure to do sizing and position after it pops up.
	
	self:SetTextLimits()
end

function FinishSaving()
	Saving = false
	DoClose = true
	LocalPlayer():SetNetworkedBool( "LimitsSent", false )
	OldSelected = ""
end
concommand.Add( "CloseLimitsPanel", FinishSaving )

function PANEL:AddRanks( Rank )
	//Let's add ranks to combobox in Init for each rank in the table
	self.RanksBox:AddItem( Rank )
end

function PANEL:AddLimitControls( Limit )
	//Let's add to a list defined in Init, using limits from tables grabbed in shared.lua
	local NLimitP = vgui.Create( "DPanel", self.LimitsList )
		function NLimitP:Paint()
			draw.RoundedBox( 4, 0, 0, NLimitP:GetWide(), NLimitP:GetTall(), Color( 25, 25, 25, 255 ) )
		end
	
	local NLimitL = vgui.Create( "DLabel", NLimitP )
		NLimitL:SetText( Limit )
		NLimitL:SizeToContents()
		NLimitL:AlignLeft( 2 )
		
	local NLimitT = vgui.Create( "DTextEntry", NLimitP )
	local RBox = self.RanksBox
		NLimitT:SetEnterAllowed( true )
		NLimitT:SetNumeric( true )
		NLimitT:Center()
		NLimitT:AlignLeft( ScrW() * 0.45 )
		function NLimitT:OnTextChanged()
			LimTable[RBox:GetSelectedItems()[1]:GetValue()][Limit] = NLimitT:GetValue()
		end
	
	
	self.LimitsList:AddItem( NLimitP )
	self.Lims[Limit] = NLimitT	//Add our text entries to a table so we can change them when hooking the Limits stream.
end

function PANEL:SetTextLimits()
	if OldSentBool then
	for k,v in pairs( LimTable[self.RanksBox:GetSelectedItems()[1]:GetValue()] ) do
		self.Lims[k]:SetValue( v )
	end
	end
end

function PANEL:PerformLayout()	//Still not dynamic at all. Meh.
	
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	self.BaseClass.PerformLayout( self )
	//Panel positioning here
	
	self.RanksBox:AlignLeft( 2 )
	self.RanksBox:AlignTop( 25 )
	self.RanksBox:SetSize( 100, self:GetTall() - 70 )
	
	self.SaveButton:AlignBottom( 2 )
	self.SaveButton:AlignLeft( 2 )
	self.SaveButton:SetSize( 100, 30 )
	
	self.LimitsList:AlignLeft( 104 )
	self.LimitsList:AlignTop( 25 )
	self.LimitsList:SetSize( self:GetWide() - 106, self:GetTall() - 30 )
	
	self:SetSize( ScrW() * 0.75, ScrH() * 0.6 )
end

function PANEL:Paint()
	self.BaseClass.Paint( self )
	//Do Painting here
	//Use PaintOver for the status message
end

function PANEL:PaintOver()	//Disable mouse while this is painted
	//Paiint over the Panel
	if !OldSentBool then
		surface.SetDrawColor( Color( 255, 255, 255, 100 ) )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
		
		surface.SetDrawColor( Color( 0, 0, 0, 25 ) )
		surface.DrawRect( self:GetWide() * 0.05, self:GetTall() * 0.05, self:GetWide() * 0.9, self:GetTall() * 0.9 )
		
		surface.SetTextColor( color_black )
		surface.SetTextPos( self:GetWide() * 0.4, self:GetTall() * 0.4 )
		surface.DrawText( "Retrieving Limits..." )
	end
	
	if Saving then
		surface.SetDrawColor( Color( 255, 255, 255, 100 ) )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
		
		surface.SetDrawColor( Color( 0, 0, 0, 25 ) )
		surface.DrawRect( self:GetWide() * 0.05, self:GetTall() * 0.05, self:GetWide() * 0.9, self:GetTall() * 0.9 )
		
		surface.SetTextColor( color_black )
		surface.SetTextPos( self:GetWide() * 0.4, self:GetTall() * 0.4 )
		surface.DrawText( "Sending Limits..." )
	end

end

function PANEL:Think()	//Add a status message on retrieving the limits.
	//Think stuff here
	if self.RanksBox:GetSelectedItems()[1] and self.RanksBox:GetSelectedItems()[1]:GetValue() != OldSelected and LTable != nil then
		OldSelected = self.RanksBox:GetSelectedItems()[1]:GetValue()
		LimTable = LTable
		self:SetTextLimits()
	end
	
	if LocalPlayer():GetNetworkedBool( "LimitsSent" ) and !OldSentBool and LTable != nil then
		LimTable = LTable
		self.RanksBox:SelectByName( "Beginner" )
		OldSentBool = true
		self:SetTextLimits()
	end
	
	if !OldSentBool then
		self:SetMouseInputEnabled( false )
	else
		self:SetMouseInputEnabled( true )
	end
	
	if DoClose then self:Close() OldSentBool, DoClose = false end
end

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel

end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "PPLimits", PANEL, "DFrame" )